//
//  NSString+PYGetScreenShotImageFromVideoPath.m
//  catuncel
//
//  Created by Jim on 2017/12/4.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "NSString+PYGetScreenShotImageFromVideoPath.h"

#import <AVFoundation/AVFoundation.h>

@implementation NSString (PYGetScreenShotImageFromVideoPath)
#pragma mark - 获取视频缩略图
/**
 *  获取视频的缩略图方法
 *  @return 视频截图
 */
- (UIImage *)getScreenShotImageFromVideoPath {
    
    UIImage *shotImage;
    
    //视频路径URL
    NSURL *fileURL = [NSURL fileURLWithPath:self];
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
    
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    
    NSError *error = nil;
    
    CMTime actualTime;
    
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
    shotImage = [[UIImage alloc] initWithCGImage:image];
    
    CGImageRelease(image);
    
    return shotImage;
    
}

@end
